package com.wl.eb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.eb.module.EbComment;
import com.wl.eb.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1") int page){
        System.out.println("进入CommentController的list方法，page:"+page);
        //1.调用service分页查询留言列表
        Page commentpage = commentService.list(page);

        //2.绑定数据到model
        model.addAttribute("commentpage",commentpage);
        //返回View
        return ("guestbook");//guestbook.html
    }

    @PostMapping
    public String save(EbComment ebComment){
        System.out.println("=============CommentController的save,comment:"+ ebComment);
        //1.调用service添加留言信息
        boolean result = commentService.save(ebComment);
        if(!result){
            throw new RuntimeException("添加留言失败！！！");
        }

        //2.绑定数据到model---不需要
        //3.返回view
        return "redirect:comment/list";//跳转http://localhost:8080/comment/list
    }

    @RequestMapping("/detail")
    public String detail(Model model, int commentId) throws Exception {
        //1.调用service根据productId查询该商品的详情
        EbComment commentDetail = commentService.detail(commentId);
        if (commentDetail == null) {
            throw new RuntimeException("查询的留言不存在");
        }
        //2.将详情数据绑定到model中
        model.addAttribute("detail", commentDetail);
        //3.返回view
        return "guestbook";//templates/news-view.html
    }
}
